package com.zp.self.module.工具.json转换.json调整;

import com.zp.self.module.工具.json转换.jsonUtil.JsonUtil;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @author ：by peng.zeng
 * @date ：Created 2021/9/3 15:05
 * @description ：整理
 */
public class Json找出多层嵌套中的值 {
    public static void main(String[] args) throws IOException {
        JSONArray mainJson = JsonUtil.getJson("C:\\Users\\peng.zeng\\Desktop\\realTime.json");
        JSONArray anew = new JSONArray();
        for (int i = 0; i < mainJson.length(); i++) {
            JSONObject json = mainJson.getJSONObject(i);
            JSONObject jsonNew = new JSONObject();
            jsonNew.put("thingId", json.get("thingId"));

            JSONObject data = json.getJSONObject("data");
            JSONObject pump_operating_time = data.getJSONObject("Pump_Operating_Time");
            JSONObject value = pump_operating_time.getJSONObject("value");
            jsonNew.put("Pump_Operating_Time", value.get("total"));

            JSONObject Mileage = data.getJSONObject("Mileage");
            JSONObject value2 = Mileage.getJSONObject("value");
            jsonNew.put("Mileage", value2.get("total"));

            if(data.has("Delivered_Amount")){
                JSONObject Delivered_Amount = data.getJSONObject("Delivered_Amount");
                JSONObject value3 = Delivered_Amount.getJSONObject("value");
                jsonNew.put("Delivered_Amount", value3.get("total"));
            }else{
                jsonNew.put("Delivered_Amount", 0);
            }

            anew.put(jsonNew);
        }

        String outPathStr = "C:\\Users\\peng.zeng\\Desktop\\realTime_ok.json";
        File file = new File(outPathStr);
        FileUtils.writeStringToFile(file, anew.toString(), "utf-8", true);
//        JsonUtil.json2csv(mainJson,"C:\\Users\\peng.zeng\\Desktop\\2022-03-01 14::00.csv");
    }
}
